package awesome.cpu.structure;

import awesome.cpu.utils.Mode;

public class FlagRegister extends SequentialComponent {
	
	public final static int KEY_FLAG_WRITE = 1;
	private int mFlagWrite;
	
	private final static boolean outputDebug = true;
	
	public final static int KEY_N = 2;
	private int mFlagN;
	public final static int KEY_Z = 3;
	private int mFlagZ;
	public final static int KEY_C = 4;
	private int mFlagC;
	public final static int KEY_V = 5;
	private int mFlagV;
	
	public final static int KEY_FLAG_ALL = 6;
	private int mFlagAll;
	
	public final static int FLAG_N = 0x08;
	public final static int FLAG_Z = 0x04;
	public final static int FLAG_C = 0x02;
	public final static int FLAG_V = 0x01;
	
	private final static FlagRegister mInstance = new FlagRegister();
	
	private FlagRegister() {}
	
	public static FlagRegister getInstance() {
		return mInstance;
	}
	
	@Override
	void onClockTick() {
		switch (mFlagWrite) {
		
		case 0:
			break;
		case 1:
			mFlagAll = 0;
			if (mFlagN == 1) mFlagAll |= FLAG_N;
			if (mFlagZ == 1) mFlagAll |= FLAG_Z;
			if (mFlagC == 1) mFlagAll |= FLAG_C;
			if (mFlagV == 1) mFlagAll |= FLAG_V;
			
			if (outputDebug && Mode.getDebugLevel() >= Mode.DEBUG_LEVEL_PRINT_ALL){
				System.out.println("flag register : N = " + mFlagN + " Z = " + mFlagZ + " C = " + mFlagC + " V = " + mFlagV);
			}
			
			break;			
		}
		
	}
	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_FLAG_WRITE:
			mFlagWrite = data;
			return true;
		case KEY_N:
			mFlagN = data;
			return true;
		case KEY_Z:
			mFlagZ = data;
			return true;
		case KEY_C:
			mFlagC = data;
			return true;
		case KEY_V:
			mFlagV = data;
			return true;
		}
		return false;
	}
	@Override
	int getData(int keyInterface) {
		switch (keyInterface) {
		case KEY_FLAG_ALL:
			return mFlagAll;
		}
		return 0;
	}
}
